<?php
/**
 * Created by PhpStorm.
 * User: zgh
 * Date: 2022-01-13
 * Time: 12:00
 */

namespace app\common\util;

use think\facade\Cache;

class CacheUtil
{
    /**
     * @param $cache_key
     * @param $func
     */
    public static function getAndSet($cache_key, $callback, $expired_time = "")
    {
        $cache = Cache::get($cache_key, "");
        if (!$cache) {
            if (is_callable($callback)) {
                $cache = call_user_func($callback);
                if (empty($expired_time)) {
                    $expired_time = 60 * 60 * 24;
                }
                Cache::set($cache_key, $cache, $expired_time);
            }
        }
        return $cache;
    }


    public static function clear($cache_key, $callback = "")
    {
        Cache::set($cache_key, null);
        if (is_callable($callback)) {
            $cache = call_user_func($callback);
        }
        return true;
    }
}